//
var elasticsearch = require('elasticsearch');
var _config = {
	host: 'localhost:9200',
	log: 'trace'
};

var client = new elasticsearch.Client({
	host: 'localhost:9200',
	log: 'trace'
});

// client.ping({
// 	requestTimeout: 30000,
// 	// undocumented params are appended to the query string
// 	hello: "elasticsearch"
// }, function(error) {
// 	if (error) {
// 		console.error('elasticsearch cluster is down!');
// 	} else {
// 		console.log('All is well');
// 	}
// });

// var _mapping = {};
var __index = 'o2o_test';
var __type = 'district';

function ElasticDao() {}

var ep = ElasticDao.prototype;

// 建立index
ep.createIndex = function() {
	client.indices.create({
		index: __index
	}).then(function(body) {
		console.log('createIndex success', body);
	}).then(function(err) {
		if (err) console.log('createIndex err', err);
	});
};

ep.closeIndex = function() {
	var self = this;
	client.indices.close({
		index: __index
	}).then(function(body) {
		console.log('success', body);
		// self.putSettings(); // fail
	}).then(function(err) {
		if (err) console.log('err', err);
	});
};

// need close the index
ep.putSettings = function() {
	client.indices.putSettings({
		index: __index,
		type: __type,
		body: {
			// "number_of_shards": 2,
			"number_of_replicas": 1
		}
	}).then(function(resp) {
		console.log('putSettings succ', resp);
	}, function(err) {
		console.trace(err.message);
	});
}

// 关于 geo
// 设置mapping  https://www.elastic.co/guide/en/elasticsearch/reference/1.6/mapping-geo-point-type.html
// 查询 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-filter.html
ep.putMapping = function() {
	client.indices.putMapping({
		index: __index,
		type: __type,
		ignoreConflicts: true,
		body: {
			"district": {
				"_all": {
					"indexAnalyzer": "ik",
					"searchAnalyzer": "ik",
					"term_vector": "no",
					"store": "false"
				},
				"properties": {
					"code": {
						"type": "long"
					},
					"pCode": {
						"type": "long"
					},
					"ts": {
						"type": "Date"
					},
					"name": {
						"type": "string",
						"store": "no",
						"term_vector": "with_positions_offsets",
						"indexAnalyzer": "ik",
						"searchAnalyzer": "ik",
						"include_in_all": "true",
						"boost": 8
					},
					"center": {
						"type": "geo_point"
					},
					"area_code": {
						"type": "long",
						"index": "not_analyzed"
					}
				}
			}
		}
	}).then(function(body) {
		console.log('putMapping success', body);
	}).then(function(err) {
		if (err) console.log('putMapping err', err);
	});
}

ep.deleteMapping = function() {
	client.indices.deleteMapping({
		index: __index,
		type: __type
	}).then(function(resp) {
		console.log('deleteMapping succ', resp);
	}, function(err) {
		console.trace(err.message);
	});
}

ep.putDoc = function() {
	client.create({
		index: __index,
		type: __type,
		id: '55a0fdb9d0485a0c2769bd5d',
		body: {
			"_id": "55a0fdb9d0485a0c2769bd5d",
			"name": "北京市",
			"code": 110000,
			"pCode": 0,
			"level": "district",
			// 注意 因为center map成了geo_point,所以[numbeer,number]
			// ["1","2"] 会出错，[1,2]才行
			"center": [
				116.405285,
				39.904989
			],
		}
	}).then(function(resp) {
		console.log('putDoc succ', resp);
	}, function(err) {
		console.trace(err.message);
	});
};


ep.search = function() {
	client.search({
		index: __index,
		type: __type,
		body: {
			query: {
				match: {
					name: '北京'
				}
			}
		}
	}).then(function(resp) {
		var hits = resp.hits.hits;
		hits.forEach(function(item) {
			console.log(item);
		});
	}, function(err) {
		console.trace(err.message);
	});
}
// 距离过滤
// GET /o2o_test/district/_search
// {
//   "query": {
//         "filtered" : {
//         "query" : {
//             "match_all" : {}
//         },
//         "filter" : {
//             "geo_distance" : {
//                 "distance" : "10km",
//                 "district.center" : [113.203425,23.071622]
//             }
//         }
//     }
//   }
// }

ep.updateDoc = function() {
	client.update({
		index: __index,
		type: __type,
		id: '15548',
		body: {
			// put the partial document under the `doc` key
			doc: {
				extra: 'from update api aaaaaaaaa'
			}
		}
	}).then(function(resp) {
		console.log('updateDoc succ', resp);
	}, function(err) {
		console.trace(err.message);
	});
};

// es.firstSync = function*() {}
// es.syncData = function*{}

// module.export = new ElasticDao();

//-------------test------------
var dao = new ElasticDao();
// dao.createIndex(); // ok
// dao.closeIndex(); // ok
// dao.putSettings(); // fail
// dao.deleteMapping();
dao.putMapping(); // ok
// dao.putDoc(); // ok
// dao.search(); // ok
// dao.updateDoc(); // ok